<template>
  <div class="app-container">
    <el-row :gutter="20">
<!--      分支数据-->
      <el-col :span="4" :xs="24">
        <div class="head-container">
          <el-input
            v-model="deptName"
            placeholder="请输入分支名称"
            clearable
            size="small"
            prefix-icon="el-icon-search"
            style="margin-bottom: 20px"
          />
        </div>
        <div class="head-container">
          <el-tree
            :data="deptOptions"
            :props="defaultProps"
            :expand-on-click-node="false"
            :filter-node-method="filterNode"
            ref="tree"
            default-expand-all
            @node-click="handleNodeClick"
          />
        </div>
        <!--          家谱列表-->
        <div class="head-container">
          <el-table
            :data="familyList"
            :expand-on-click-node="false"
            :filter-node-method="filterNode"
            ref="form"
            default-expand-all
            @row-click="handleFamNodeClick"
          >
            <el-table-column label="包含家谱" align="center" prop="famName"/>
          </el-table>
        </div>
      </el-col>
      <!--      成员数据-->
      <el-col :span="20" :xs="24">
        <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch"
                 label-width="68px">
          <el-form-item label="成员姓名" prop="memberName">
            <el-input
              v-model="queryParams.memberName"
              placeholder="请输入成员姓名"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item label="成员年龄" prop="memberAge">
            <el-input
              v-model="queryParams.memberAge"
              placeholder="请输入成员年龄"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item label="成员性别" prop="memberSex">
            <el-select v-model="queryParams.memberSex" placeholder="请选择成员性别" clearable>
              <el-option
                v-for="dict in dict.type.sys_user_sex"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="出生日期" prop="birthDate">
            <el-date-picker clearable
                            v-model="queryParams.birthDate"
                            type="date"
                            value-format="yyyy-MM-dd"
                            placeholder="请选择出生日期">
            </el-date-picker>
          </el-form-item>
          <el-form-item label="去世日期" prop="deadDate">
            <el-date-picker clearable
                            v-model="queryParams.deadDate"
                            type="date"
                            value-format="yyyy-MM-dd"
                            placeholder="请选择去世日期">
            </el-date-picker>
          </el-form-item>
          <el-form-item label="辈分" prop="gender">
            <el-input
              v-model="queryParams.gender"
              placeholder="请输入辈分"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item label="遗传病" prop="disease">
            <el-input
              v-model="queryParams.disease"
              placeholder="请输入遗传病"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-form>

        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button
              type="primary"
              plain
              icon="el-icon-plus"
              size="mini"
              @click="handleAdd"
              v-hasPermi="['system:member:add']"
            >新增
            </el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button
              type="success"
              plain
              icon="el-icon-edit"
              size="mini"
              :disabled="single"
              @click="handleUpdate"
              v-hasPermi="['system:member:edit']"
            >修改
            </el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button
              type="danger"
              plain
              icon="el-icon-delete"
              size="mini"
              :disabled="multiple"
              @click="handleDelete"
              v-hasPermi="['system:member:remove']"
            >删除
            </el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button
              type="warning"
              plain
              icon="el-icon-download"
              size="mini"
              @click="handleExport"
              v-hasPermi="['system:member:export']"
            >导出
            </el-button>
          </el-col>
          <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>

        <el-table v-loading="loading" :data="memberList" @selection-change="handleSelectionChange">
          <el-table-column type="selection" width="55" align="center"/>
          <el-table-column label="成员编号" align="center" prop="memberId"/>
          <el-table-column label="成员姓名" align="center" prop="memberName"/>
          <el-table-column label="成员年龄" align="center" prop="memberAge"/>
          <el-table-column label="成员性别" align="center" prop="memberSex">
            <template slot-scope="scope">
              <dict-tag :options="dict.type.sys_user_sex" :value="scope.row.memberSex"/>
            </template>
          </el-table-column>
          <el-table-column label="出生日期" align="center" prop="birthDate" width="180">
            <template slot-scope="scope">
              <span>{{ parseTime(scope.row.birthDate, '{y}-{m}-{d}') }}</span>
            </template>
          </el-table-column>
          <el-table-column label="去世日期" align="center" prop="deadDate" width="180">
            <template slot-scope="scope">
              <span>{{ parseTime(scope.row.deadDate, '{y}-{m}-{d}') }}</span>
            </template>
          </el-table-column>
          <el-table-column label="辈分" align="center" prop="gender"/>
          <el-table-column label="个人传记" align="center" prop="memberBiography"/>
          <el-table-column label="遗传病" align="center" prop="disease"/>
          <el-table-column label="所属家谱" align="center" prop="famId"/>
          <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
            <template slot-scope="scope">
              <el-button
                size="mini"
                type="text"
                icon="el-icon-edit"
                @click="handleUpdate(scope.row)"
                v-hasPermi="['system:member:edit']"
              >修改
              </el-button>
              <el-button
                size="mini"
                type="text"
                icon="el-icon-delete"
                @click="handleDelete(scope.row)"
                v-hasPermi="['system:member:remove']"
              >删除
              </el-button>
            </template>
          </el-table-column>
        </el-table>

        <pagination
          v-show="total>0"
          :total="total"
          :page.sync="queryParams.pageNum"
          :limit.sync="queryParams.pageSize"
          @pagination="getList"
        />
      </el-col>
    </el-row>
    <!-- 添加或修改家谱成员对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="成员姓名" prop="memberName">
          <el-input v-model="form.memberName" placeholder="请输入成员姓名"/>
        </el-form-item>
        <el-row>
          <el-col :span="12">
            <el-form-item label="归属家族" prop="deptId">
              <treeselect v-model="form.deptId" :options="deptOptions" :show-count="true" placeholder="请选择归属家族" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="所属族谱" prop="famId">
              <el-select v-model="form.famId" :options="familyList" :show-count="true" placeholder="请选择所属家谱">
              <el-option
                v-for="item in familyList"
                :key="item.famId"
                :label="item.famName"
                :value="item.famId"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="成员年龄" prop="memberAge">
          <el-input v-model="form.memberAge" placeholder="请输入成员年龄"/>
        </el-form-item>
        <el-form-item label="成员性别" prop="memberSex">
          <el-select v-model="form.memberSex" placeholder="请选择成员性别">
            <el-option
              v-for="dict in dict.type.sys_user_sex"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-row>
          <el-col :span="12">
            <el-form-item label="父亲">
              <el-select v-model="form.fatherId" placeholder="请选择父亲">
                <el-option
                  v-for="item in maleOptions"
                  :key="item.memberId"
                  :label="item.memberName"
                  :value="item.memberId"
                  :disabled="item.status == 1"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="母亲">
              <el-select v-model="form.motherId" placeholder="请选择母亲">
                <el-option
                  v-for="item in femaleOptions"
                  :key="item.memberId"
                  :label="item.memberName"
                  :value="item.memberId"
                  :disabled="item.status == 1"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>

        <el-form-item label="出生日期" prop="birthDate">
          <el-date-picker clearable
                          v-model="form.birthDate"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="请选择出生日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="去世日期" prop="deadDate">
          <el-date-picker clearable
                          v-model="form.deadDate"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="请选择去世日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="辈分" prop="gender">
          <el-input v-model="form.gender" placeholder="请输入辈分"/>
        </el-form-item>
        <el-form-item label="个人传记" prop="memberBiography">
          <el-input v-model="form.memberBiography" type="textarea" placeholder="请输入内容"/>
        </el-form-item>
        <el-form-item label="遗传病" prop="disease">
          <el-input v-model="form.disease" placeholder="请输入遗传病"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {listMember, getMember, delMember, addMember, updateMember} from "@/api/system/member";
import Treeselect from "@riophae/vue-treeselect";
import {treeselect} from "@/api/system/dept";
import {listFamily, getFamily} from "@/api/system/family";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";

export default {
  name: "Member",
  dicts: ['sys_user_sex'],
  components: {Treeselect},
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 分支树选项
      deptOptions: undefined,
      //家谱列表选择
      famOptions: undefined,
      // 家谱表格数据
      familyList: [],
      // 父亲选项
      maleOptions: [],
      // 母亲选项
      femaleOptions: [],
      // 家谱成员表格数据
      memberList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        memberName: null,
        memberAge: null,
        memberSex: null,
        birthDate: null,
        deadDate: null,
        fatherId: null,
        motherId: null,
        partnerId: null,
        gender: null,
        memberBiography: null,
        disease: null,
        famId: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        memberName: [
          {required: true, message: "成员姓名不能为空", trigger: "blur"}
        ],
      }
    };
  },
  created() {
    this.getList();
    this.getTreeselect();
    this.getFamList();
  },
  methods: {
    /** 查询家谱成员列表 */
    getList() {
      this.loading = true;
      listMember(this.queryParams).then(response => {
        this.memberList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 查询家谱列表 */
    getFamList() {
      this.loading = true;
      listFamily(this.queryParams).then(response => {
        this.familyList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 查询家谱列表 */
    getListselect() {
      listFamily().then(response => {
        this.famOptions = response.data;
      });
    },
    /** 查询部门下拉树结构 */
    getTreeselect() {
      treeselect().then(response => {
        this.deptOptions = response.data;
      });
    },
    /** 查询家谱下拉树结构 */
    getFamTreeselect() {
      famtreeselect().then(response => {
        this.famOptions = response.data;
      });
    },
    /** 转换分支数据结构 */
    normalizer(node) {
      if (node.children && !node.children.length) {
        delete node.children;
      }
      return {
        id: node.deptId,
        label: node.deptName,
        children: node.children
      };
    },
    // 筛选节点
    filterNode(value, data) {
      if (!value) return true;
      return data.label.indexOf(value) !== -1;
    },
    // 分支节点单击事件
    handleNodeClick(data) {
      this.queryParams.deptId = data.id;
      this.handleFamQuery();
    },
    // 家谱节点单击事件
    handleFamNodeClick(row) {
      console.log(row)
      this.queryParams.famId = row.famId;
      this.handleQuery();
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        memberId: null,
        memberName: null,
        memberAge: null,
        memberSex: null,
        birthDate: null,
        deadDate: null,
        fatherId: null,
        motherId: null,
        partnerId: null,
        gender: null,
        memberBiography: null,
        disease: null,
        famId: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    handleFamQuery(){
      this.queryParams.pageNum = 1;
      this.getFamList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.memberId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {

      getMember().then(response => {
        console.log(response)
        this.open = true;
        this.maleOptions = response.males;
        this.femaleOptions = response.females;
        this.getTreeselect();
        this.getFamList();

        this.title = "添加家谱成员";
        this.reset();

      });

    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      const memberId = row.memberId || this.ids
      getMember(memberId).then(response => {
        console.log(response)
        this.open = true;
        this.maleOptions = response.males;
        this.femaleOptions = response.females;
        this.form.fatherId = response.fatherId;
        this.form.motherId = response.motherId;
        this.title = "修改家谱成员";
        this.form = response.data;
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.memberId != null) {
            updateMember(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addMember(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const memberIds = row.memberId || this.ids;
      this.$modal.confirm('是否确认删除家谱成员编号为"' + memberIds + '"的数据项？').then(function () {
        return delMember(memberIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/member/export', {
        ...this.queryParams
      }, `member_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
